package edu.umn.se.trap.logicChecker;

import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

import edu.umn.se.trap.Day;
import edu.umn.se.trap.Meal;
import edu.umn.se.trap.Incidental;
import edu.umn.se.trap.Lodging;
import edu.umn.se.trap.Form;
import edu.umn.se.trap.exceptions.DayException;

public class CheckSyntaxDay implements Observer {

    @Override
    /**
     * Update for checking the syntax of per diem
     */
    public void update(Observable o, Object arg) {
       
        Form form = (Form) arg;    
        LogicChecker l1 = (LogicChecker) o;

        Vector<Day> days = form.getDays();

        for (int i = 0; i < days.size(); i++)
        {
            Day day = days.elementAt(i);
            Meal breakfast = day.getBreakfast();
            Meal lunch = day.getLunch();
            Meal dinner = day.getDinner();
            Incidental incidental = day.getIncidentals();
            Lodging lodge = day.getLodging();
            
            if (breakfast.getCountry().equalsIgnoreCase("usa")
                    && breakfast.getCountry().equalsIgnoreCase("united states"))
                    {
                        if (breakfast.getState().equals(""))
                        {
                            l1.setException(new DayException("Error: No state provided if country is USA"));
                        }
                    }
            if (!breakfast.getState().equals(""))
            {
                if (!breakfast.getCountry().equalsIgnoreCase("usa")
                        && !breakfast.getCountry().equalsIgnoreCase("united states"))
                {
                    l1.setException(new DayException("Error: Check over country and state." + dinner.getCountry() + " " + dinner.getState()));
                }
            }
            if (lunch.getCountry().equalsIgnoreCase("usa")
                    && lunch.getCountry().equalsIgnoreCase("united states"))
                    {
                        if (lunch.getState().equals(""))
                        {
                            l1.setException(new DayException("Error: No state provided if country is USA"));
                        }
                    }
            if (!lunch.getState().equals(""))
            {
                if (!lunch.getCountry().equalsIgnoreCase("usa")
                        && !lunch.getCountry().equalsIgnoreCase("united states"))
                {
                    l1.setException(new DayException("Error: Check over country and state."));
                }
            }
            
            if (dinner.getCountry().equalsIgnoreCase("usa")
                    && dinner.getCountry().equalsIgnoreCase("united states"))
                    {
                        if (dinner.getState().equals(""))
                        {
                            l1.setException(new DayException("Error: Not state provided if country is USA"));
                        }
                    }
            if (!dinner.getState().equals(""))
            {
                if (!dinner.getCountry().equalsIgnoreCase("usa")
                        && !dinner.getCountry().equalsIgnoreCase("united states"))
                {
                    l1.setException(new DayException("Error: Check over country and state." + dinner.getCountry() + " " + dinner.getState()));
                }
            }
            
            if (incidental.getCountry().equalsIgnoreCase("usa")
                    && incidental.getCountry().equalsIgnoreCase("united states"))
                    {
                        if (incidental.getState().equals(""))
                        {
                            l1.setException(new DayException("Error: Must have a state if country is USA"));
                        }
                    }

            if (!lodge.getState().equals(""))
            {
                if (!lodge.getCountry().equalsIgnoreCase("usa")
                        && !lodge.getCountry().equalsIgnoreCase("united states"))
                {
                    l1.setException(new DayException("Error: Check over country and state."));
                }
            }
            
        }
    }
}